<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php
session_start();
require_once 'code/Tasks.php';
require_once 'code/Util.php';
?>
<html>
    <head>
        <link rel="shortcut icon" href="images/iTrack.ico"> 
        <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="PRIVATE">
        <META HTTP-EQUIV="CONTENT-LANGUAGE" CONTENT="en-US">
        <META NAME="DESCRIPTION" CONTENT="Tasks application home page">
        <META NAME="KEYWORDS" CONTENT="iTrack">
        <META NAME="COPYRIGHT" CONTENT="&copy; iTrack 2013">

        <title>iTrack - Projects</title>

        <link href="assets/css/bootstrap.css" rel="stylesheet">
        <style type="text/css">
            body {
                padding-top: 60px;
            }
            #sortable { list-style-type: none; margin: 0; padding: 0; width: 100%; }
            #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.0em; height: auto; }
            #sortable li span { position: absolute; margin-left: -1.3em; }            
            .sections {
                padding-top: 2em;
            }
            .sectionHeader {
                color:black;
            }
            .items {
                color:#4E4E52;
            } 
            .placeholder { height: 2.5em !important; line-height: 2.5em !important; }
        </style>
        <link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
        <script src="assets/js/jquery.js"></script>        
        <script type="text/javascript" src="jquery/jquery-ui-1.8.13.custom.min.js"></script>


        <script type="text/javascript">
            $(document).ready(function() {
                $( "#sortable" ).sortable({
                    placeholder: "alert alert-error placeholder",
                    update: function( event, ui ) {
                        var sortedIDs = $( "#sortable" ).sortable( "toArray" );
                        //alert(sortedIDs.toString());
                        // Make ajax call to save the changed sorting order
                        UpdateSortOrder(sortedIDs);
                    }
                });
                $( "#sortable" ).disableSelection();      
            });
            
            /* Update tasks sorting order on server, using displayIndex */
            function UpdateSortOrder(sortedIDs) {
                var projectid = $("#projectid").val();
                var userid = $("#userid").val();
		
                
                $.ajax({         
                    url: "controller.php", 
                    type: "GET",
                    dataType: "json",                
                    data: { 
                        "action": "updatetasksorder",
                        "userid": userid,
                        "projectid":projectid,
                        "priorities" : sortedIDs.toString()
                    },         
                    success: function(data) { 
                        if (data != null) {
                            if(data.status == "success") {
                                window.location ="viewproject.php?projectid="+projectid;
                            } else {
                                //alert("Error updating tasks sort order on server !");
                            }
                        }                                                               
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert("Error updating tasks sort order on server ! " + thrownError);
                    }
                });
		
		
            }
	
            function CreateProjectPopup() {
                document.getElementById("txtProjectName").focus();
                $('#myModal').modal('show');
            }
		
            function SaveProject() {
                var projectid = $("#projectid").val();
                var projectname = $("#txtProjectName").val();
                var projectdesc = $("#txtProjectDescription").val();
                var visibility = $("input:radio[name=Visibility]:checked").val();
		
                if(projectname != "") {
                    $.ajax({         
                        url: "controller.php", 
                        type: "POST",
                        dataType: "json",                
                        data: { 
                            "action": "saveproject",
                            "userid": $("#userid").val(),
                            "projectid":projectid,
                            "projectname" : projectname,
                            "description" : projectdesc,
                            "visibility"  : visibility
                        },         
                        success: function(data) { 
                            if (data != null) {
                                if(data.status == "success") {
                                    $('#myModal').modal('hide');
                                    window.location ="viewproject.php?projectid="+projectid;
                                } else {
                                    alert("Error saving project !");
                                }
                            }                                                               
                        },
                        error:function (xhr, ajaxOptions, thrownError){
                            //window.location = "error.php";
                        }
                    });
                }
                return false;
            }
	
            function ViewProject(projectid) {
                //alert("Project Id = " + projectid);
                if((projectid != null) &&(projectid != "0")) {
                    window.location = "viewproject.php?projectid=" + projectid;
                } 		
            }
	
            function DeleteProject() {
                if(confirm("Deleting Project. Are you sure?") == true) {
                    $.ajax({         
                        url: "../Controller", 
                        type: "POST",
                        dataType: "json",                
                        data: { 
                            "action": "deleteproject",
                            "userid": $("#userid").val(),
                            "projectid":$("#projectid").val()
                        },         
                        success: function(data) { 
                            if (data != null) {
                                if(data.status == "success") {
                                    $('#myModal').modal('hide');
                                    //window.location ="projects.php" + "?random="+new Date().getTime();
                                } else {
                                    alert("Error deleting project !");
                                }
                            }                                                               
                        },
                        error:function (xhr, ajaxOptions, thrownError){
                            //window.location ="projects.php" + "?random="+new Date().getTime();
                        }
                    });
                }
		
                return false;
            }
	
            function MarkComplete(taskid) {
                // Mark task as completed
                //alert('Task ID = ' + taskid);
		
                $.ajax({         
                    url: "../Controller", 
                    type: "POST",
                    dataType: "json",                
                    data: { 
                        "action": "changeTaskStatus",
                        "taskstatus" : "Done",
                        "userid": $("#userid").val(),
                        "projectid":$("#projectid").val(),
                        "taskid": taskid
                    },         
                    success: function(data) { 
                        if (data != null) {
                            if(data.status == "success") {
                                $('#myModal').modal('hide');
                                window.location ="viewproject.php" + "?projectid=" + $("#projectid").val();
                            } else {
                                alert("Error updating task status !");
                            }
                        }                                                               
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        window.location ="viewproject.php" + "?projectid=" + $("#projectid").val();
                    }
                });
            }
	
            function PrintPreview() {
                window.print();
            }             
        </script>
        <style type="text/css">
            .sidebar-nav {
                padding: 9px 0;
            }
            .listcontents {
                padding:10px;
            }
        </style>    
    </head>
    <body>
        <?php
        if (isset($_SESSION['userid'])) {
            $userid = $_SESSION['userid'];
            $projectid = $_GET['projectid'];
            ?>

            <input type="hidden" id="userid" name="userid" value="<?php echo $_SESSION['userid']; ?>">
            <input type="hidden" id="projectid" name="projectid" value="<?php echo $_GET['projectid']; ?>">

            <?php
            Util::getNavBar();
            ?>

            <div class="container-fluid">
                <div class="row-fluid">
                    <div class="span2">
                        <div class="well sidebar-nav">
                            <?php
                            echo Util::getLeftNavList('Projects');
                            ?>
                        </div><!--/.well -->
                    </div><!--/span-->
                    <div class="span10">          
                        <div class="row-fluid">
                            <div class="span12">
                                <?php
                                $tasksObj = new Tasks();
                                $projects = array();

                                if ($projectid > 0) {
                                    $projects = $tasksObj->getAllProjects($userid, $projectid);
                                }

                                $projectname = "N/A";
                                $description = "N/A";
                                //echo count($projects);
                                if (count($projects) > 0) {
                                    foreach ($projects as $db_field) {
                                        $projectname = $db_field['project'];
                                        $description = $db_field['description'];
                                    }
                                    ?>
                                    <h2><a href="viewproject.php?projectid=<?php echo $projectid ?>"><?php echo $projectname ?></a>
                                        &nbsp;&nbsp;<a class="btn btn-mini" type="button" href="javascript:void(0);" onclick="CreateProjectPopup()"><i class="icon icon-edit"></i>&nbsp; Edit</a>
                                    </h2>
                                    <p class="muted"><?php echo $description ?></p>

                                    <div class="sections">
                                        <p class="text-info" style="font-size: 1.3em;"><a class="sectionHeader" href="javascript:void(0)"><strong>Tasks</strong></a>

                                        <div class="btn-toolbar" style="margin: 0;">
                                            <div class="btn-group">
                                                <a class="btn btn-info" type="button" href="createtask.php?projectid=<?php echo $projectid ?>"><i class="icon icon-plus-sign"></i>&nbsp; Add task</a>							  
                                            </div>

                                            <div class="btn-group">
                                                <a class="btn dropdown-toggle btn-info" data-toggle="dropdown" href="#">
                                                    <i class="icon icon-list"></i>&nbsp;Sort
                                                    <span class="caret"></span>
                                                </a>
                                                <ul class="dropdown-menu">
                                                    <li><a href="javascript:void(0);">Created By</a></li>
                                                    <li><a href="javascript:void(0);">Due Date</a></li>
                                                    <li><a href="javascript:void(0);">Priority</a></li>
                                                    <li><a href="javascript:void(0);">Status</a></li>
                                                    <li><a href="javascript:void(0);">Type</a></li>
                                                </ul>
                                            </div> 
                                        </div>

                                        </p>
                                    </div>

                                    <div>
                                        <table cellpadding="4" cellspacing="0" border="0">
                                            <tr>
                                                <td>	                				
                                                </td>
                                                <td width="100%">
                                                    <ul id="sortable">
                                                        <?php
                                                        if ($projectid > 0) {
                                                            $tasks = $tasksObj->getTasks($userid, "All", $projectid);
                                                        }

                                                        foreach ($tasks as $db_field) {
                                                            ?>

                                                            <li id="<?php echo $db_field['id'] ?>" class="alert alert-success" style="vertical-align: bottom">
            <!--                                                        <span class="icon-fullscreen"></span>-->
                                                                <input type="checkbox" id="task_<?php echo $db_field['id'] ?>" value="<?php echo $db_field['id'] ?>"  
                                                                       title="Mark completed" onclick="MarkComplete('<?php echo $db_field['id'] ?>')">
                                                                &nbsp;&nbsp;
                                                                <a class="items" href="edittask.php?projectid=<?php echo $projectid ?>&taskid=<?php echo $db_field['id'] ?>"><?php echo $db_field['description'] ?></a> 


                                                                <?php
                                                                /*
                                                                  if(tasks.get(i).getComments().size() > 0) {
                                                                  if(tasks.get(i).getComments().size() == 1) {

                                                                 */
                                                                ?>
                        <!--&nbsp; &nbsp; &nbsp; <span class="label"><%= tasks.get(i).getComments().size() %> comment</span> -->
                                                                <?php
                                                                /* 	} else { */
                                                                ?>
                        <!--&nbsp; &nbsp; &nbsp; <span class="label"><%= tasks.get(i).getComments().size() %> comments</span>-->
                                                                <?php
                                                                /*
                                                                  }
                                                                  }

                                                                 */
                                                                ?>
                                                            </li>								

                                                        <?php }
                                                        ?>
                                                    </ul>								
                                                    <?php
                                                } else {
                                                    echo '<div class="text-error">Either project doesn\'t exist or you are not authorized for viewing !</div>';
                                                }
                                                ?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>

                                            </td>
                                            <td>
                                                <p class="muted"><a style="color:#969696;" href="javascript:void(0)"><em><?php echo $tasksObj->getDoneTaskCount($projectid, $userid) ?> completed tasks</em></a></p>
                                            </td>
                                        </tr>               		
                                    </table>
                                </div>                

                            </div><!--/span-->          
                        </div><!--/row-->

                    </div><!--/span-->
                </div><!--/row-->

                <!-- Include footer -->
                <?php include_once 'footer.php'; ?>
            </div>


            <!-- Dialogs -->
            <div class="modal hide fade in" id="myModal" tabindex="-1"
                 role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
                 style="width: 25em">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">&times;</button>
                    <h3 id="myModalLabel"><i class="icon-bullhorn"></i>&nbsp;Edit Project</h3>
                </div>
                <div class="modal-body">
                    <div class="control-group">
                        <div class="controls">
                            <label class="control-label" for="txtProjectName"><abbr
                                    title="Project Name">Project Name</abbr></label>
                            <div class="controls">
                                <input type="text" class="input-xlarge" id="txtProjectName"
                                       name="txtProjectName" autofocus value="<?php echo $projectname ?>">
                            </div>
                        </div>
                        <div class="controls">
                            <label class="control-label" for="txtProjectDescription"><abbr
                                    title="Project Description">Description</abbr></label>
                            <div class="controls">
                                <textarea class="input-xlarge" id="txtProjectDescription"
                                          name="txtProjectDescription" rows="5"><?php echo $description ?></textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-danger" onclick="DeleteProject()">Delete</button>
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-primary" onclick="SaveProject()">Save</button>
                </div>
            </div>


            <!-- Placed at the end of the document so the pages load faster -->            
            <script src="assets/js/bootstrap-transition.js"></script>
            <script src="assets/js/bootstrap-alert.js"></script>
            <script src="assets/js/bootstrap-modal.js"></script>
            <script src="assets/js/bootstrap-dropdown.js"></script>
            <script src="assets/js/bootstrap-scrollspy.js"></script>
            <script src="assets/js/bootstrap-tab.js"></script>
            <script src="assets/js/bootstrap-tooltip.js"></script>
            <script src="assets/js/bootstrap-popover.js"></script>
            <script src="assets/js/bootstrap-button.js"></script>
            <script src="assets/js/bootstrap-collapse.js"></script>
            <script src="assets/js/bootstrap-carousel.js"></script>
            <script src="assets/js/bootstrap-typeahead.js"></script>

            <?php
        } else {
            header("location: login.php");
        }
        ?> 
    </body>
</html>